package com.bq.server.base;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
import java.util.stream.Collectors;

/**
 * bean 父接口,封装bean的操作
 *
 * @author: ChenSH
 * @date: Create by in 14:38 2019-05-27
 */
public interface SuperBean {

    /**
     * bean的所有属性转换成map
     */
    default Map<String, Object> toMap() {
        return BeanUtil.beanToMap(this);
    }

    /**
     * bean 属性复制给目标对象,并返回模板对象
     *
     * @param target 目标对象实例
     * @param <T>    目标对象类型
     * @return 目标对象
     */
    default <T> T copyProperties(T target) {
        BeanUtil.copyProperties(this, target);
        return target;
    }

    /**
     * @see BeanUtil#copyProperties(Object, Object, String...)
     */
    default <T> T copyProperties(T target, String... ignoreProperties) {
        BeanUtil.copyProperties(this, target, ignoreProperties);
        return target;
    }

    static <T extends SuperBean, R> List<R> listCopyProperties(List<T> sourceList, Supplier<? extends R> supplier, String... ignoreProperties) {
        if (CollUtil.isEmpty(sourceList)) {
            return Collections.emptyList();
        }
        return sourceList.stream()
                .map(source -> source.copyProperties(supplier.get(), ignoreProperties))
                .collect(Collectors.toList());
    }
}
